import os
from pathlib import Path
import shutil
import re

file_dir="E:\周丹丽研究生\语言学python\语料分组"
pattern_blank=" "#文件夹的名字（其实是前后不能带）不带空格
for root,dirs,files in os.walk(file_dir):
	list_none=[]
	if files==list_none:
		continue
	
	p = Path(root)  # 初始化构造Path对象
	#print(root)

	file_object_list = list(p.glob("./*_out.txt"))
	for file in file_object_list:
		str_filename=file.name
		#print(str_filename)
		start_index=0
		str_gender=""
		for i in range(0,len(str_filename)):
			if str_filename[i]=='%':
				start_index=i
				break
		
		for i in range(start_index+1,len(str_filename)):
			if str_filename[i]=='%':
				start_index=i
				break
		#print(start_index)
		for i in range(start_index+1,len(str_filename)):
			if str_filename[i]=='%':
				break
			str_gender=str_gender+str_filename[i]
		if str_gender=="":
			str_gender="unknown"
		#print(str_gender)
		
		folder=os.path.exists(root+'\\'+str_gender)
		
		if not folder:
			os.makedirs(root+'\\'+str_gender)
			
			shutil.copy(root+'\\'+file.name,root+'\\'+str_gender+'\\'+file.name)
		else:	
			#print(root+'\\'+file.name)
			shutil.copy(root+'\\'+file.name,root+'\\'+str_gender+'\\'+file.name)

	file_object_list = list(p.glob("./*_chuli.txt"))
	for file in file_object_list:
		str_filename=file.name
		#print(str_filename)
		start_index=0
		str_gender=""
		for i in range(0,len(str_filename)):
			if str_filename[i]=='%':
				start_index=i
				break
		
		for i in range(start_index+1,len(str_filename)):
			if str_filename[i]=='%':
				start_index=i
				break
		#print(start_index)
		for i in range(start_index+1,len(str_filename)):
			if str_filename[i]=='%':
				break
			str_gender=str_gender+str_filename[i]
		if str_gender=="":
			str_gender="unknown"
		#print(str_gender)
		
		folder=os.path.exists(root+'\\'+str_gender)
		
		if not folder:
			os.makedirs(root+'\\'+str_gender)
			
			shutil.copy(root+'\\'+file.name,root+'\\'+str_gender+'\\'+file.name)
		else:	
			#print(root+'\\'+file.name)
			shutil.copy(root+'\\'+file.name,root+'\\'+str_gender+'\\'+file.name)
		